package com.itic.appbase.framework.common.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.itic.appbase.framework.common.constants.ResponseCode;
import com.itic.appbase.framework.common.persistence.dao.HtCrudDao;
import com.itic.appbase.framework.common.persistence.model.BaseExample;
import com.itic.appbase.framework.common.persistence.model.HtBaseModel;

/**
 * 增删改查基本类，提供单表的增删改查功能.
 * <p>
 * Company: itic
 * </p>
 * 
 * @author: idong
 * @date: 2015年5月8日 下午12:43:14
 * @version: V1.0
 * @param <S>
 * @param <M>
 * @param <E>
 */
public class HtCrudService<S extends HtCrudDao<M, E>, M extends HtBaseModel, E extends BaseExample>
        extends HtBaseService {
    @Autowired
    protected S mapper;
    // return code
    protected static final String SUCCESS = ResponseCode.SUCCESS.getCode();
    protected static final String FAILED = ResponseCode.SERVICE_ERROR.getCode();

    /**
     * 查询记录总数.
     * 
     * @param example
     * @return
     * @throws
     */
    public int count(E example) {
        return mapper.countByExample(example);
    }

    /**
     * 根据example查询列表数据.
     * 
     * @param example
     * @return
     * @throws
     */
    public List<M> list(E example) {
        return mapper.selectByExample(example);
    }

    /**
     * 根据ID查询指定数据.
     * 
     * @param id
     * @return
     * @throws
     */
    public M get(Integer id) {
        if ( null != id ) {
            return mapper.selectByPrimaryKey(id);
        }
        return null;
    }

    /**
     * 新增model数据.
     * 
     * @param model
     * @return
     * @throws
     */
    public String add(M model) {
        int code = mapper.insert(this.initDefalutFields(model));
        if (1 == code) {
            return SUCCESS;
        } else if (0 == code) {
            return FAILED;
        } else {
            return FAILED;
        }
    }

    /**
     * 根据ID更新指定数据.
     * 
     * @param model
     * @return
     * @throws
     */
    public String edit(M model) {
        int code = 0;
        Integer id = model.getId();
        if (null != id ) {
            if (null == model.getMtime()) {
                model.setMtime(new Date());
            }
            code = mapper.updateByPrimaryKeySelective(model);
        }
        if (1 == code) {
            return SUCCESS;
        } else if (0 == code) {
            return FAILED;
        } else {
            return FAILED;
        }
    }

    /**
     * 根据ID删除指定数据.
     * 
     * @param model
     * @return
     * @throws
     */
    public String delete(M model) {
        int code = 0;
        Integer id = model.getId();
        if ( null != id ) {
            if (null == model.getMtime()) {
                model.setMtime(new Date());
            }
            /* if (null == model.getIsEffective() || StringHelper.isBlank(model.getIsEffective())) {
                model.setIsEffective(DBConst.FALSE);
            }*/
            model.setDelFlag(0);
            code = mapper.updateByPrimaryKeySelective(model);
        }
        if (1 == code) {
            return SUCCESS;
        } else if (0 == code) {
            return FAILED;
        } else {
            return FAILED;
        }
    }


}
